package com.lzx.adx.report.util.biz.product;

import cn.hutool.core.util.URLUtil;
import com.lzx.adx.report.dto.AccountDTO;
import com.lzx.adx.report.dto.ReportDTO;
import com.lzx.adx.report.util.URLEncoderDecoderTool;
import org.apache.commons.lang3.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class HuihuangChannelHelper {



    public static String covertUrlReport(AccountDTO accountDTO, ReportDTO reportDTO) {
        Map<String, Object> prams = new HashMap<>();
        prams.put("os", "0");
        prams.put("ip", StringUtils.isNotBlank(reportDTO.getIp())?reportDTO.getIp():"");
        prams.put("tms", reportDTO.getTraceTime());
        if (StringUtils.isNotBlank(reportDTO.getUser_agent())) {
            prams.put("adAgent", URLEncoderDecoderTool.encodeURL(reportDTO.getUser_agent()));
        }

        if (StringUtils.isNotBlank(reportDTO.getOaid()) && !reportDTO.getOaid().contains("__")) {
            prams.put("oaid", reportDTO.getOaid());
        }
        if(StringUtils.isNotBlank(reportDTO.getOaidSum()) && !reportDTO.getOaidSum().contains("__")) {
            prams.put("oaidMd5", reportDTO.getOaidSum());
        }

        if(StringUtils.isNotBlank(reportDTO.getImeiSum()) && !reportDTO.getImeiSum().contains("__")) {
            prams.put("imeiMd5", reportDTO.getImeiSum());
        }
        if(StringUtils.isNotBlank(reportDTO.getIdfa()) && !reportDTO.getIdfa().contains("__")){
            prams.put("idfa", reportDTO.getIdfa());
        }
        if(StringUtils.isNotBlank(reportDTO.getIdfaSum()) && !reportDTO.getIdfaSum().contains("__")){
            prams.put("idfaMd5", reportDTO.getIdfaSum());
        }

        String callBackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        prams.put("callbackUrl", URLEncoderDecoderTool.encodeURL(callBackUrl));
        String paramsUrl = URLUtil.buildQuery(prams, StandardCharsets.UTF_8);
        return accountDTO.getDspReportUrl() + "&" + paramsUrl;
    }
}
